package com.lijin.study.java.thread.pool1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.lijin.study.java.thread.task.ReadFileTask;
import com.lijin.study.java.thread.task.Task;
import com.lijin.study.java.thread.task.UpperTask;

public class ThreadPool {
	private LinkedList<Task> tasks = new LinkedList<Task>();
	
	private List<ThreadTask> threads = new ArrayList<ThreadTask>();

	public ThreadPool(int size) {
		for (int i = 0; i < size; i++) {
			ThreadTask thread = new ThreadTask(this, i);
			thread.start();
			threads.add(thread);
		}
	}

	public void run(Task task) {
		synchronized (tasks) {
			tasks.addLast(task);
			tasks.notify();
		}
	}

	public Task getNext() {
		Task returnVal = null;
		synchronized (tasks) {
			while (tasks.isEmpty()) {
				try {
					tasks.wait();
				} catch (InterruptedException ex) {
					System.err.println("Interrupted");
				}
			}
			returnVal = tasks.removeFirst();
		}
		return returnVal;
	}
	
	public boolean isAllStopped() {
		for (ThreadTask t : threads) {
			if (t.isStillWorking()) {
				return false;
			}
		}
		return true;
	}
	
	public void killAll() {
		for (ThreadTask t : threads) {
			t.interrupt();
			System.out.println(t.getId() + " is killed..."); 
		}
	}

	public static void main(String args[]) throws InterruptedException {
//		String message[] = { "Java", "Source", "and", "Support" };
//		ThreadPool pool = new ThreadPool(message.length);
//		long start = System.currentTimeMillis();
//		for (int i = 0; i < 10; i++) {
//			Task task = null;
//			if (i % 4 == 0) {
//				task = new UpperTask(message[i%4]);
//			} else {
//				task = new LowerTask(message[i%4]);
//			}
//			pool.run(task);
//		}
//		while (true) {
//			if (pool.isAllStopped()) {
//				System.out.println("执行时间：" + (System.currentTimeMillis() - start));
//				pool.killAll();
//				break;
//			}
//		}
		
		long start = System.currentTimeMillis();
		
		ThreadPool pool = new ThreadPool(2);
		
		pool.run(new ReadFileTask("latest_half_year_2.txt"));
		pool.run(new ReadFileTask("latest_half_year_result.txt"));
		pool.run(new ReadFileTask("MC00000001100529_GNETE"));
//		pool.run(new ReadFileTask("MC59609940110321_BOC_GNETE"));
//		pool.run(new ReadFileTask("Mont_2_result.txt"));
//		pool.run(new ReadFileTask("test.csv"));
//		pool.run(new ReadFileTask("trans_cd.txt"));
//		pool.run(new ReadFileTask("TARGET"));
		pool.run(new UpperTask("Just a test"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		pool.run(new UpperTask("xxxxx"));
		
		while (true) {
			if (pool.isAllStopped()) {
				System.out.println("执行时间：" + (System.currentTimeMillis() - start));
				pool.killAll();
				break;
			}
		}
		System.out.println("-----End-----" + Thread.currentThread().getName());
	}
}
